package com.hexiang.web;

import com.hexiang.domain.bo.BookBo;
import com.hexiang.domain.vo.BookRequestVO;
import com.hexiang.domain.ResultResponse;
import com.hexiang.service.BookService;
import com.hexiang.validator.*;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.math.BigDecimal;

/**
 * 书籍控制器
 *
 * @author hexiang
 * @version 1.0
 */
@RestController
@Validated
public class BookController {
    @Resource
    private BookService bookService;

    @PostMapping("/addBook")
    public ResultResponse addBook(BookRequestVO bookRequestVO) {
        BookBo bookBo=new BookBo();
        bookBo.setName(bookRequestVO.getName());
        bookBo.setAuthor(bookRequestVO.getAuthor());
        bookBo.setPrice(bookRequestVO.getPrice());
        bookService.addBook(bookBo);
        return ResultResponse.success("新增书籍成功", null);
    }

    /**
     * 新增书籍
     *
     * @param newBook com.hexiang.domain.Book
     * @return com.hexiang.domain。ResultResponse
     */
    @PostMapping("/createBook")
    public ResultResponse createBook(@Validated(value = CreateBookGroup.class) @UniqueBookName() @AuthorRestrict(message = "作者不被允许") @RequestBody BookRequestVO newBook) {
        return ResultResponse.success("新增书籍成功", newBook);
    }

    /**
     * 修改书籍
     *
     * @param book com.hexiang.domain.Book
     * @return com.hexiang.domain。ResultResponse
     */
    @PostMapping("/updateBook")
    public ResultResponse updateBook(@Validated(value = UpdateBookGroup.class) @RequestBody BookRequestVO book) {
        return ResultResponse.success("修改书籍成功", book);
    }

    /**
     * 查询单个书籍
     *
     * @param id 书籍id
     * @return com.hexiang.domain。ResultResponse
     */
    @GetMapping("/books/{id}")
    public ResultResponse getBook(@PathVariable @Min(1) @Max(10) Long id) {
        BookRequestVO book = new BookRequestVO();
        book.setId(id);
        book.setName("test");
        book.setAuthor("Santideva");
        book.setPrice(new BigDecimal("29.9"));
        return ResultResponse.success("查询书籍成功", book);
    }

    /**
     * 删除书籍
     *
     * @param book com.hexiang.domain.Book
     * @return com.hexiang.domain。ResultResponse
     */
    @PostMapping("deleteBook")
    public ResultResponse deleteBook(@Validated(value = DeleteBookGroup.class) @RequestBody BookRequestVO book) {
        return ResultResponse.success("删除书籍成功", null);
    }


}
